import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { FunctionOutputDto, FunctionInputDto } from './model';
import { AjaxResult, TreeNode } from '@/api/model';
import { PageRequest, PageData } from '@/utils/common/types';

/** 管理-功能信息枚举 */
enum Api {
  /** 读取功能信息 */
  Read = '/api/admin/function/read',
  /** 读取功能[模块]树数据 */
  ReadTreeNode = '/api/admin/function/readTreeNode',

  /** 更新功能信息 */
  Update = '/api/admin/function/update',
}

//#region 读取功能信息
/**
 * 读取功能信息
 * @param request 分页请求
 * @param mode 错误消息模型
 */
export function readFunctions(request: PageRequest, mode: ErrorMessageMode = 'message') {
  return httpClient.post<PageData<FunctionOutputDto>>(
    {
      url: Api.Read,
      params: request,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
//#region 读取功能[模块]树数据
/**
 * 读取功能[模块]树数据
 * @param moduleId 模块编号
 * @param mode 错误消息模型
 */
export function readFunctionTreeNodes(
  moduleId: string | bigint,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.get<TreeNode[]>(
    {
      url: Api.ReadTreeNode,
      params: { moduleId: moduleId },
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion

//#region 更新功能信息
/**
 * 更新功能信息
 * @param dtos 功能信息
 * @param mode 错误消息模型
 */
export function updateFunctions(dtos: FunctionInputDto[], mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.Update,
      params: dtos,
    },
    {
      errorMessageMode: mode,
    },
  );
}
//#endregion
